<?php
/**
 * Created by ThinkPHP5.0.22.
 * User: 小涛.
 * Date: 2018/12/27.
 * Time: 22:50.
 */

namespace app\admin\controller;

use app\admin\model\News as NewsModel;
use think\Request;
use app\admin\model\NewsType;
use think\Validate;


/**
 * Class News  资讯列表
 * @package app\admin\controller
 */
class News extends Admin
{
    //列表
    public function index(){
        $request = Request::instance();
        $member_id = 0;
        $data = $request -> param();
        if(array_key_exists("member_id",$data)) { $member_id = $data["member_id"]; }
        $list = NewsModel::listNews($member_id,10);
        $this->assign("list",$list);
        $this->assign("member_id",$member_id);
        return $this -> fetch();
    }

    //添加
    public function add(){
        $request = Request::instance();
        if($request -> isPost()){
            if($this->verify_authority() != ""){
                return $this->verify_authority();
            }
            $data = $request -> param();
            $validate = new Validate([
                ["title","require","文章资讯标题不能为空"],
                ["news_type","require|number|gt:0","请选择文章资讯类型|请选择文章资讯类型|请选择文章资讯类型"],
                ["logo","require|number|gt:0","请上传图片|请上传图片|请上传图片"],
                ["author","require","文章资讯作者不能为空"],
                ["brief","require","文章资讯简介不能为空"],
                ["content","require","文章资讯内容不能为空"],
            ]);
            if(!$validate->check($data)){
                return $validate -> getError();
            }
            $data["create_time"] = time();
            $data["update_time"] = time();
            if($data["sort"] <= 0){ $data["sort"] = 255; }
            $add = new NewsModel($data);
            if($add -> allowField(true) -> save()){
                return "添加成功";
            }
            return "添加失败";
        }
        $news_type = NewsType::order("sort asc") -> select();
        $this->assign("news_type",$news_type);
        return $this->fetch();
    }

    //编辑
    public function edit(){
        $request = Request::instance();
        $data = $request -> param();
        $id = 0;
        if(array_key_exists("id",$data)){
            $id = $data["id"];
        }
        if($request -> isPost()){
            if($this->verify_authority() != ""){
                return $this->verify_authority();
            }
            $validate = new Validate([
                ["title","require","文章资讯标题不能为空"],
                ["news_type","require|number|gt:0","请选择文章资讯类型|请选择文章资讯类型|请选择文章资讯类型"],
                ["logo","require|number|gt:0","请上传图片|请上传图片|请上传图片"],
                ["author","require","文章资讯作者不能为空"],
                ["brief","require","文章资讯简介不能为空"],
                ["content","require","文章资讯内容不能为空"],
            ]);
            if(!$validate->check($data)){
                return $validate -> getError();
            }
            if($data["sort"] <= 0){ $data["sort"] = 255; }
            $data["update_time"] = time();
            $news = new NewsModel();
            $news = $news -> allowField(true) -> save($data,["id" => $id]);
            if($news){
                return "编辑成功";
            }
            return "编辑失败";
        }
        $list = NewsModel::get($id);
        if(!$list || $list["news_type"] == 0){
            return $this -> redirect("admin/news/index");
        }
        $this->assign("list",$list);
        $news_type = NewsType::order("sort asc") -> select();
        $this->assign("news_type",$news_type);
        return $this->fetch();
    }

    //删除
    public function delete(){
        $request = Request::instance();
        if($request -> isPost()){
            if ($this->verify_authority() != "") {
                return $this->verify_authority();
            }
            $data = $request -> param();
            $id = 0;
            if(array_key_exists("id",$data)){
                $id = $data["id"];
            }
            $delete = NewsModel::get($id);
            if($delete -> delete()){
                return "删除成功";
            }
            return "删除失败";
        }
    }

}